package com.maiji.cloud.utils;

import java.util.Date;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.maiji.cloud.request.shopingmall.AlipayRefundReqData;
import com.maiji.cloud.request.shopingmall.AlipayRefundReqData.RefundReqInner;
import com.maiji.cloud.utils.AlipayUtil;
import com.maiji.cloud.utils.TimeUtil;
import com.maiji.cloud.utils.WXUtil;

public class AlipayRefund {
	public static void main(String[] args) throws Exception  {
		String nowTime = TimeUtil.date2String(new Date(), null);
		AlipayRefundReqData aliPayResData = new AlipayRefundReqData();
		aliPayResData.setApp_id(AlipayUtil.APP_ID);
		aliPayResData.setCharset("utf-8");
		aliPayResData.setMethod("alipay.trade.refund");
		aliPayResData.setSign_type("RSA2");
		aliPayResData.setTimestamp(nowTime);
		aliPayResData.setVersion("1.0");
		RefundReqInner aliPayResInner = aliPayResData.dataInstance();
		aliPayResInner.setOut_trade_no("12536185809433416049");
		aliPayResInner.setRefund_amount(0.12);
		aliPayResInner.setTrade_no("2019091922001493240583643390");
		Map paramMap = WXUtil.objectToMap(aliPayResData);
		paramMap.remove("biz_content");
		paramMap.put("biz_content", JSON.toJSON(aliPayResInner));
		
		String signString = AlipayUtil.createSign(paramMap);
		String sign = AlipaySignature.rsaSign(signString, AlipayUtil.PRIVATE_KEY, "utf-8", "RSA2");
		aliPayResData.setSign(sign);
		paramMap.put("sign", sign);
		AlipayClient alipayClient = new DefaultAlipayClient
				("https://openapi.alipay.com/gateway.do",AlipayUtil.APP_ID,AlipayUtil.PRIVATE_KEY,"json","utf-8",AlipayUtil
				.PUBLIC_KEY,"RSA2");
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		request.setBizContent( JSON.toJSONString(aliPayResInner));
		AlipayTradeRefundResponse response = alipayClient.execute(request);
		System.out.println(JSON.toJSONString(response));
		if(response.isSuccess()){
		System.out.println("调用成功");
		} else {
		System.out.println("调用失败");
		}
		
//	AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",AlipayUtil.APP_ID,AlipayUtil.PRIVATE_KEY,"json","utf-8",AlipayUtil
//			.PUBLIC_KEY,"RSA2");
//	
//		AlipayRefundReqData aliPayResData = new AlipayRefundReqData();
//		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
//		RefundReqInner aliPayResInner = aliPayResData.dataInstance();//这里我封装了一个类用来传递json数据
//		aliPayResInner.setRefund_amount(0.12);
//		aliPayResInner.setOut_trade_no("12536185809433416049");
//
//		aliPayResInner.setTrade_no("2019091922001493240583643390");
//		request.setBizContent(JSON.toJSONString(aliPayResInner));//将数据格式化成json格式
//		System.out.println("sdsjdssssss"+JSON.toJSONString(aliPayResInner));
//		AlipayTradeRefundResponse response;
//		try {
//			response = alipayClient.execute(request);
//			
//			System.out.println("LLLLLLLLLLLLLL"+JSON.toJSONString(response));
//		} catch (AlipayApiException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		
		
//		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
//		request.setBizContent("{" +
//		"\"out_trade_no\":\"12536185809433416049\"," +
//		"\"trade_no\":\"2019091922001493240583643390\"," +
//		"\"refund_amount\":0.12" +
//		"  }");
//		AlipayTradeRefundResponse response;
//		try {
//			response = alipayClient.execute(request);
//		} catch (AlipayApiException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		if(response.isSuccess()){
//		System.out.println("调用成功");
//		} else {
//		System.out.println("调用失败");
//		}
	}
}
